package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.DictionaryDepartmentMapper;
import com.hamster.crm.mapper.DictionaryPositionMapper;
import com.hamster.crm.mapper.EmployeeMapper;
import com.hamster.crm.mapper.EmployeeStateMapper;
import com.hamster.crm.pojo.DictionaryDepartment;
import com.hamster.crm.pojo.DictionaryPosition;
import com.hamster.crm.pojo.Employee;
import com.hamster.crm.pojo.EmployeeState;
import com.hamster.crm.service.EmployeeService;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 后台员工管理的service
 * @author Tarry
 * @create 2019/12/21 15:15
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DictionaryDepartmentMapper dictionaryDepartmentMapper;
    @Autowired
    private DictionaryPositionMapper dictionaryPositionMapper;
    @Autowired
    private EmployeeStateMapper employeeStateMapper;
    /**
     * 新增员工
     * @param employee
     */
    @Override
    public void insert(Employee employee) {
        employee.setInputTime(new Date());
        employee.setDel("0");
        employee.setInputUser("功能待完善");

        if (StringUtils.isNotBlank(employee.getSuperiorId())){
            String superName = employeeMapper.selectById(employee.getSuperiorId()).getName();
            employee.setSuperiorName(superName);
        }
        if (StringUtils.isNotBlank(employee.getRecruiterId())){
            String recruiterName = employeeMapper.selectById(employee.getRecruiterId()).getName();
            employee.setRecruiter(recruiterName);
        }

        employeeMapper.insert(employee);
    }

    /**
     * 根据id获取员工信息
     * @param id
     * @return
     */
    @Override
    public Employee get(String id) {
        return employeeMapper.selectById(id);
    }

    /**
     * 修改员工信息
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        if (StringUtils.isNotBlank(employee.getSuperiorId())){
            String superName = employeeMapper.selectById(employee.getSuperiorId()).getName();
            employee.setSuperiorName(superName);
        }
        if (StringUtils.isNotBlank(employee.getRecruiterId())){
            String recruiterName = employeeMapper.selectById(employee.getRecruiterId()).getName();
            employee.setRecruiter(recruiterName);
        }

        if (StringUtils.isBlank(employee.getSuperiorId())){
            employee.setSuperiorId(" ");
            employee.setSuperiorName(" ");
        }

        employee.setInputUser("功能的待完善"); //记录操作人
        employee.setInputTime(new Date());   //记录操作时间，当前系统时间
        employeeMapper.updateById(employee);
    }

    /**
     * 因为删除员工，让手机端也不能使用，所以这里通过把手机号字段变的不可用来解决。
     * 根据id删除员工记录
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<String> ids) {
        String inputUser = "功能待完善"; //记录操作人
        Date date = new Date();         //记录操作时间，当前系统时间
        Long time1 = date.getTime();
        String time = "#"+time1.toString();
        //因为删除员工，让手机端也不能使用，所以这里通过把手机号字段变的不可用来解决。
        for (String id : ids) {
            employeeMapper.deleteTag(id,inputUser,date,time); //设置删除标记为1
        }
    }

    /**
     * 添加查询 员工列表
     * @param pageSize    每页条数
     * @param currentPage 当前页
     * @param name  姓名
     * @param startTime 入职时间 开始
     * @param endTime 入职时间 终止
     * @param department 部门
     * @return
     */
    @Override
    public PageResult<Employee> list(Integer pageSize, Integer currentPage, String name, Date startTime,Date endTime, String department) {
        //分页
        Page<Employee> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("del","0").orderByDesc("join_time");
        //根据姓名过滤
        if (StringUtils.isNotBlank(name)){
            wrapper.like("name","%"+name+"%");
        }
        //根据部门过滤
        if (StringUtils.isNotBlank(department)){
            wrapper.eq("department",department);
        }

        //根据招聘时间过滤
        if (startTime != null && endTime != null){
            wrapper.ge("join_time",startTime);
            wrapper.le("join_time",endTime);
        }

        IPage pageList = employeeMapper.selectPage(page, wrapper);
        Long pages = pageList.getPages();
        List<Employee> currentList = pageList.getRecords();
        return new PageResult<Employee>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 根据 职位和部门获取上级领导的下拉列表
     * @param departmentId 部门id
     * @param positionId   职位id
     * @return
     */
    @Override
    public List<Employee> getSuperiorSelect(String departmentId, String positionId) {
        Integer posNum = Integer.valueOf(positionId);
        positionId = String.valueOf(posNum + 1);

        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.select("id","name","phone","position");
        employeeQueryWrapper.eq("department",departmentId);//当前部门
        employeeQueryWrapper.eq("position",positionId);//当前级别高一级的员工

        //employeeQueryWrapper.eq("employee_state","0"); //0 在职  1离职
        employeeQueryWrapper.eq("del","0");//0 正常   1已删除

        return employeeMapper.selectList(employeeQueryWrapper);
    }

    /**
     * 获取部门下拉框
     * @return
     */
    @Override
    public List<DictionaryDepartment> getDepartmentSelect() {
        QueryWrapper<DictionaryDepartment> dictionaryDepartmentQueryWrapper = new QueryWrapper<>();
        List<DictionaryDepartment> dictionaryDepartments = dictionaryDepartmentMapper.selectList(dictionaryDepartmentQueryWrapper);
        return dictionaryDepartments;
    }

    /**
     * 获取部门下拉框【服务部事业部】
     * @return
     */
    @Override
    public List<DictionaryDepartment> getDepartmentSelect2() {
        QueryWrapper<DictionaryDepartment> dictionaryDepartmentQueryWrapper = new QueryWrapper<>();
        dictionaryDepartmentQueryWrapper.or().eq("id","2").or().eq("id","3");
        List<DictionaryDepartment> dictionaryDepartments = dictionaryDepartmentMapper.selectList(dictionaryDepartmentQueryWrapper);
        return dictionaryDepartments;
    }

    @Override
    public List<Employee> recruiterList() {
        //"开发部_1", "服务部_2","事业部_3","财务部_4","后勤部_5","人事部_6"
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.select("id","name");
        employeeQueryWrapper.eq("del","0").eq("department","6");//人事部员工列表
        List<Employee> list = employeeMapper.selectList(employeeQueryWrapper);
        return list;
    }


    /**
     * 获取职位下拉框
     * @return
     */
    @Override
    public List<DictionaryPosition> getPositionSelect(String departmentId) {
        if ("2".equals(departmentId) || "3".equals(departmentId)){
            QueryWrapper<DictionaryPosition> dictionaryPositionQueryWrapper = new QueryWrapper<>();
            dictionaryPositionQueryWrapper.eq("id","2").or().eq("id","3");
            List<DictionaryPosition> dictionaryPositions = dictionaryPositionMapper.selectList(dictionaryPositionQueryWrapper);
            return dictionaryPositions;
        }

        QueryWrapper<DictionaryPosition> dictionaryPositionQueryWrapper = new QueryWrapper<>();
        List<DictionaryPosition> dictionaryPositions = dictionaryPositionMapper.selectList(dictionaryPositionQueryWrapper);
        return dictionaryPositions;
    }

    @Override
    public List<Employee> queryAllEmployee() {
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("del","0");
        return employeeMapper.selectList(employeeQueryWrapper);
    }

    /**
     * 员工状态 下拉框
     * @return
     */
    @Override
    public List<EmployeeState> employeeStateList() {
      return employeeStateMapper.selectList(new QueryWrapper<EmployeeState>());
    }

}
